package com.example.permission.controller;

import com.example.permission.common.page.Page;
import com.example.permission.common.result.Result;
import com.example.permission.entity.dto.SysDeptDTO;
import com.example.permission.entity.dto.page.SysDeptPageDTO;
import com.example.permission.entity.po.SysDept;
import com.example.permission.entity.vo.SysDeptTreeVO;
import com.example.permission.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 部门信息
 *
 * @author xuwq
 * @date 2020-05-09 15:56:07
 */
@RestController
@Api(tags = "部门信息")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @PostMapping("v1/dept")
    @ApiOperation("新增")
    public Result<SysDept> add(@RequestBody @Valid SysDeptDTO sysDeptDTO) {
        return Result.success(this.sysDeptService.insert(sysDeptDTO));
    }

    @PutMapping("v1/dept/{deptId}")
    @ApiOperation("修改")
    public Result<SysDept> edit(@PathVariable("deptId") Integer deptId, @RequestBody @Valid SysDeptDTO sysDeptDTO) {
        sysDeptDTO.setDeptId(deptId);
        return Result.success(this.sysDeptService.update(sysDeptDTO));
    }

    @DeleteMapping("v1/dept/{deptId}")
    @ApiOperation("删除")
    public Result<Boolean> deleteById(@PathVariable("deptId") Integer deptId) {
        return Result.success(this.sysDeptService.deleteById(deptId));
    }

    @GetMapping("v1/dept/{deptId}")
    @ApiOperation(value = "根据id查询数据")
    public Result<SysDept> queryById(@PathVariable("deptId") Integer deptId) {
        return Result.success(this.sysDeptService.queryById(deptId));
    }

    @PostMapping("v1/dept/multi")
    @ApiOperation(value = "查询列表")
    public Result<Page<SysDept>> queryListByPage(@RequestBody SysDeptPageDTO sysDeptPageDTO) {
        return Result.success(this.sysDeptService.queryListByPage(sysDeptPageDTO));
    }

    @GetMapping("v1/dept/tree/{pid}")
    @ApiOperation(value = "查询部门树")
    public Result<List<SysDept>> queryDeptTree(@PathVariable("pid") Integer pid) {
        return Result.success(this.sysDeptService.queryDeptTree(pid));
    }

    @GetMapping("v1/dept/tree")
    @ApiOperation(value = "查询部门树")
    public Result<List<SysDeptTreeVO>> queryDeptTreeAll() {
        return Result.success(this.sysDeptService.queryDeptTree());
    }
}